﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_ManageNews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDataGrid(1);
        }
    }

    protected void grdNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdNews.PageIndex = e.NewPageIndex;
        grdNews.DataBind();
    }
    protected void grdNews_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string strNewsID = e.CommandArgument.ToString().Trim();
        string strCommand = e.CommandName;
        switch (strCommand)
        {

            case "delete_News":
                try
                {
                    News news = News_session.Get_News_ByID(int.Parse(strNewsID));
                    if (news == null) return;
                    News_session.Delete_News(news);

                   // string _sFolder = Request.PhysicalApplicationPath.Replace(@"\", "/") + "/Images/Product/";
                   // ExtendFunctional.DeleteImageInFolder(_sFolder + "/" + news.News_Image);
                    WebMsgBox.Show("Đã xóa thành công");
                    BindDataGrid(1);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                break;
            case "active_News":
                try
                {
                    News news = News_session.Get_News_ByID(int.Parse(strNewsID));
                    if (news == null) return;

                    news.News_Status = !news.News_Status;
                    News_session.Update_News(news);
                    BindDataGrid(1);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                break;
        }
    }

    private void BindDataGrid(int p)
    {
        grdNews.DataSource = News_session.GetList_News();
        grdNews.DataBind();
    }
    protected void btnDeleteAll_Click(object sender, EventArgs e)
    {
        
    }
    //Lay ID tren cac cot duoc checked	
    private string NewsSelectedArray()
    {
        string strArrayID = "";
        foreach (GridViewRow rows in grdNews.Rows)
        {
            CheckBox chk = (CheckBox)rows.FindControl("chkCheck");

            if (chk.Checked)
            {
                ImageButton btnDel = (ImageButton)rows.FindControl("btnDelete");
                if (strArrayID != "")
                {
                    strArrayID += ",";
                }
                strArrayID += btnDel.CommandArgument.Trim();
            }
        }
        return strArrayID;
    }

    protected void grdNews_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton imgActive = (ImageButton)e.Row.FindControl("btnActive");
            if (imgActive != null)
            {
                object obj = DataBinder.Eval(e.Row.DataItem, "News_Status");
                if (obj != null)
                {
                    if (obj.ToString().ToLower() == "true")
                    {
                        imgActive.ImageUrl = "Images/check.gif";
                    }
                    else
                    {
                        imgActive.ImageUrl = "Images/uncheck.gif";
                    }
                }
            }
        }
    }
}
